package com.yimi.appstore.util;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

public class AboutMobileInfoTools {

	private static String TAG = "AboutMobileInfoTools";
	private static Context context = null;
	public HashMap<String, String> deviceInfoMap = null;

	public void setContext(Context context) {
		this.context = context;
	}

	/**
	 * 类级内部类，在首次被访问时才会被初始化
	 */
	private static class SingleHolder {
		static void setContext(Context context) {
			objsingle.setContext(context);
		}

		/**
		 * 静态初始化器，有JVM来保证线程安全
		 */
		private static AboutMobileInfoTools objsingle = new AboutMobileInfoTools();
	}

	/**
	 * 单例，获得接口实现
	 * 
	 * @return
	 */
	public static AboutMobileInfoTools getInstance(Context context) {

		SingleHolder.setContext(context);

		return SingleHolder.objsingle;
	}

	/**
	 * 单例，获得接口实现
	 * 
	 * @return
	 */
	public static AboutMobileInfoTools getInstance() {
		return SingleHolder.objsingle;
	}

	public HashMap<String, String> getDeviceInfoMap() {
		if (deviceInfoMap == null) {
			deviceInfoMap = new HashMap<String, String>();
			deviceInfoMap.put("user-id", getUserId());
			deviceInfoMap.put("moble-id", getImei());
			deviceInfoMap.put("moble-os", getSDKVersion());
			deviceInfoMap.put("app-version", getVersionName());
			deviceInfoMap.put("carrier", getOperator());
			deviceInfoMap.put("network", getNetworkType());
			deviceInfoMap.put("moble-model", getDeviceType());
			deviceInfoMap.put("versionCode", ""+getVersionCode());
		}
		Log.d(TAG, "getDeviceInfoMap:" + deviceInfoMap);
		return deviceInfoMap;

	}

	public String getDeviceInfoStr() {
		StringBuffer sber = new StringBuffer();
		sber.append(getUserId()).append(",").append(getImei()).append(",").append(getSDKVersion()).append(",")
				.append(getVersionName()).append(",").append(getOperator()).append(",").append(getNetworkType())
				.append(",").append(getDeviceType());

		Log.d(TAG, "getDeviceInfoStr()->sber=" + sber.toString());
		return sber.toString().trim();
	}

	/**
	 * 获取用户的userid 如果没有则返回0
	 * 
	 * @return
	 */
	public  String getUserId() {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = tm.getSubscriberId();
		return imsi;
	}

	/**
	 * 获得sdk版本号
	 * 
	 * @return
	 */
	public  String getSDKVersion() {
		String sdkVersion = "";
		try {
			sdkVersion = android.os.Build.VERSION.RELEASE.trim();
		} catch (Exception e) {
			sdkVersion = "3";
		}

		return sdkVersion;
	}

	/**
	 * 获得versionname
	 * 
	 * @return
	 */
	public  String getVersionName() {
		PackageManager pm = context.getPackageManager();
		PackageInfo info = null;
		try {
			info = pm.getPackageInfo(context.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return "1.0.0";
		}
		Log.i(TAG, "getVersionName()-->versionname=" + info.versionName);

		return info.versionName;

	}

	/**
	 * 获得versioncode
	 * 
	 * @return
	 */
	public  int getVersionCode() {
		PackageManager pm = context.getPackageManager();
		PackageInfo info = null;
		try {
			info = pm.getPackageInfo(context.getPackageName(), 0);
			return info.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return 1;
		}
	}

	/**
	 * 获得imei
	 * 
	 * @return
	 */
	public  String getImei() {
		String imei = "";
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		imei = tm.getDeviceId();
		return imei;
	}

	/**
	 * 获得运营商
	 * 
	 * @return
	 */
	public  String getOperator() {
		String operator = "";
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imsi = tm.getSubscriberId();
		if (imsi != null) {
			if (imsi.startsWith("46000") || imsi.startsWith("46002")) {// 因为移动网络编号46000下的IMSI已经用完，所以虚拟了一个46002编号，134/159号段使用了此编号
				// 中国移动
				operator = "中国移动";
			} else if (imsi.startsWith("46001")) {
				// 中国联通
				operator = "中国联通";
			} else if (imsi.startsWith("46003")) {
				// 中国电信
				operator = "中国电信";
			}
		}
		return operator;
	}

	/**
	 * 获得网络类型
	 * 
	 * @return
	 */
	public  String getNetworkType() {
		String type = "unknown";
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connec.getActiveNetworkInfo();
		if (info != null) {
			if (info.getType() == ConnectivityManager.TYPE_WIFI) {
				type = "WIFI";
			} else {
				int id = tm.getNetworkType();
				if (TelephonyManager.NETWORK_TYPE_CDMA == id || TelephonyManager.NETWORK_TYPE_GPRS == id
						|| TelephonyManager.NETWORK_TYPE_EDGE == id) {
					type = "2G";
				} else if (TelephonyManager.NETWORK_TYPE_UMTS == id || TelephonyManager.NETWORK_TYPE_HSDPA == id
						|| TelephonyManager.NETWORK_TYPE_EVDO_A == id || TelephonyManager.NETWORK_TYPE_EVDO_0 == id) {
					type = "3G";
				} else {
					type = "WIFI";
				}
			}
		}

		return type;
	}

	/**
	 * 获得设备型号
	 * 
	 * @return
	 */
	public  String getDeviceType() {
		String info = Build.MANUFACTURER + Build.MODEL;
		return info;
	}

	static DisplayMetrics dm = new DisplayMetrics();
	/**
	 * 获取屏幕宽带
	 * 
	 * @return
	 */
	public  int getScreenWidth() {
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	/**
	 * 获取屏幕高
	 * 
	 * @return
	 */
	public  int getScreenHeight() {
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		return  dm.heightPixels;
	}

	/**
	 * 获取屏幕密度
	 * 
	 * @return
	 */
	public int getScreenDensity() {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.densityDpi;
	}

	/**
	 * 获取mac地址
	 * 
	 * @return
	 */
	public  String getLocalMac() {
		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = (wifiManager == null ? null : wifiManager.getConnectionInfo());
		if (info != null)
			return info.getMacAddress();
		return "";
	}

	/**
	 * 获取包名
	 * 
	 * @return
	 */
	public   String getPckName() {
		return context.getPackageName();
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
}
